Software Development Exam  >  Software Development Questions  >  employees = [ {name: John, age: 25, departme... Start Learning for Free
employees = [
    {'name': 'John', 'age': 25, 'department': 'Sales'},
    {'name': 'Emma', 'age': 30, 'department': 'HR'},
    {'name': 'Mike', 'age': 35, 'department': 'IT'}
]
department_names = [employee['department'] for employee in employees]
department_set = set(department_names)
print(len(department_set))
What will be the output of the above code?
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    Error: 'list' object has no attribute 'department'
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
employees = [ {name: John, age: 25, department: Sales}, {name: Emma,...
The code creates a list of department names by extracting the 'department' value from each employee dictionary in the employees list. The department names are then converted into a set to eliminate duplicates. The length of the set department_set is 3.
View all questions of this test
Most Upvoted Answer
employees = [ {name: John, age: 25, department: Sales}, {name: Emma,...
Output Explanation:

The given code defines a list of dictionaries called "employees" that contains information about three employees. Each dictionary represents an employee and contains keys for "name", "age", and "department".

Then, the code uses a list comprehension to extract the value of the "department" key from each dictionary in the "employees" list. These department names are stored in a new list called "department_names".

Next, the code creates a set called "department_set" from the "department_names" list. A set is an unordered collection of unique elements, so it will eliminate any duplicate department names.

Finally, the code prints the length of the "department_set" set, which represents the number of unique department names.

Output:

The output of the code will be 3.

Explanation:

The code defines a list of dictionaries representing three employees with different departments. When the department names are extracted from the dictionaries and stored in the "department_names" list, we get the following list:

["Sales", "HR", "IT"]

Since each department name is unique, when we convert this list to a set using the set() function, the resulting set will only contain the unique elements. In this case, the set will be:

{"Sales", "HR", "IT"}

The set contains three unique elements, which means there are three different departments among the employees.

Therefore, when we print the length of the "department_set" set using the len() function, the output will be 3.
Explore Courses for Software Development exam

Top Courses for Software Development

Question Description
employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer?.
Solutions for employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice employees = [ {name: John, age: 25, department: Sales}, {name: Emma, age: 30, department: HR}, {name: Mike, age: 35, department: IT}]department_names = [employee[department] for employee in employees]department_set = set(department_names)print(len(department_set))What will be the output of the above code?a)1b)2c)3d)Error: list object has no attribute departmentCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev